package com.liuxin.zhiyuan.shujuzhongxin.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.liuxin.zhiyuan.shujuzhongxin.domain.po.XueyuanInfoEntity;
import com.liuxin.zhiyuan.shujuzhongxin.service.XueyuanInfoService;
import com.liuxin.common.utils.PageUtils;
import com.liuxin.common.utils.R;



/**
 * 学生志愿填报信息表（用于志愿填报分析）
 *
 * @author zsl
 * @email 1301966496@qq.com
 * @date 2025-11-08 22:16:26
 */
@RestController
@RequestMapping("shujuzhongxin/xueyuaninfo")
public class XueyuanInfoController {
    @Autowired
    private XueyuanInfoService xueyuanInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("shujuzhongxin:xueyuaninfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = xueyuanInfoService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    //@RequiresPermissions("shujuzhongxin:xueyuaninfo:info")
    public R info(@RequestParam("id") Integer id){
		XueyuanInfoEntity xueyuanInfo = xueyuanInfoService.getById(id);
        return R.ok().put("xueyuanInfo", xueyuanInfo);
    }

    /**
     * 新增学生志愿信息（带业务验证）
     */
    @PostMapping("/add")
    //@RequiresPermissions("shujuzhongxin:xueyuaninfo:add")
    public R add(@RequestBody XueyuanInfoEntity xueyuanInfo){
        System.out.println(xueyuanInfo);
        // 业务验证：检查学生姓名是否已存在
        if (xueyuanInfo.getStudentName() == null || xueyuanInfo.getStudentName().trim().isEmpty()) {
            return R.error("学生姓名不能为空");
        }
        // 业务验证：检查高考成绩是否合理
        if (xueyuanInfo.getExamScore() == null) {
            return R.error("高考成绩不能为空");
        }
        // 业务验证：检查高考成绩是否合理
        if (xueyuanInfo.getExamScore() != null && (xueyuanInfo.getExamScore() < 0 || xueyuanInfo.getExamScore() > 750)) {
            return R.error("高考成绩必须在0-750分之间");
        }
        // 业务验证：检查手机号格式
        if (xueyuanInfo.getPhone() != null && !xueyuanInfo.getPhone().matches("^1[3-9]\\d{9}$")) {
            return R.error("手机号码格式不正确");
        }
        // 业务验证：检查高考位次不能为空
        if (xueyuanInfo.getExamRank() == null) {
            return R.error("高考位次不能为空");
        }
        // 设置默认值
        if (xueyuanInfo.getCreateTime() == null) {
            xueyuanInfo.setCreateTime(new java.util.Date());
        }
        if (xueyuanInfo.getUpdateTime() == null) {
            xueyuanInfo.setUpdateTime(new java.util.Date());
        }
        // 业务验证：检查手机号是否已存在
        if (xueyuanInfo.getPhone() != null && xueyuanInfoService.isPhoneExists(xueyuanInfo.getPhone(), null)) {
            return R.error("手机号已存在，请使用其他手机号");
        }
        // 保存数据
        boolean result = xueyuanInfoService.save(xueyuanInfo);
        if (result) {
            return R.ok().put("id", xueyuanInfo.getId()).put("message", "新增成功");
        } else {
            return R.error("新增失败");
        }
    }
    /**
     * 修改
     */
    @PostMapping("/up")
    //@RequiresPermissions("shujuzhongxin:xueyuaninfo:update")
    public R update(@RequestBody XueyuanInfoEntity xueyuanInfo){
		xueyuanInfoService.updateById(xueyuanInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("shujuzhongxin:xueyuaninfo:delete")
    public R delete(@RequestBody Integer[] ids){
		xueyuanInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}